rm(list = ls())


# Purpose: 
#  Build prior data for phi from the Consolidation of Democracy in CEE survey data. 
#
#
# 
#


eurobaro <- read.dta("./datasource/cee_survey_short.dta")
eurobaro <- eurobaro[, c("id", "voteint" ,"lrs")]
eurobaro$lrs <- as.numeric(eurobaro$lrs)
eurobaro$lrs[eurobaro$lrs > 10] <- NA
eurobaro$lrs <- scale(eurobaro$lrs, scale=F)	
eurobaro <- na.omit(eurobaro)



eurobaro$party_family <- 8
eurobaro$party_family[eurobaro$voteint>99 & eurobaro$voteint<200] <-  6  # "Communists"
eurobaro$party_family[eurobaro$voteint>199 & eurobaro$voteint<300] <- 5  # "Socialists" 
eurobaro$party_family[eurobaro$voteint>299 & eurobaro$voteint<400] <- 3  # "Liberals" 
eurobaro$party_family[eurobaro$voteint>399 & eurobaro$voteint<500] <- 4  # "Christian Democrats"
eurobaro$party_family[eurobaro$voteint>499 & eurobaro$voteint<600] <- 2  # "Conservatives"
eurobaro$party_family[eurobaro$voteint>599 & eurobaro$voteint<700] <- 1  # "Extreme Right / Nationalism"
eurobaro$party_family[eurobaro$voteint>799 & eurobaro$voteint<900] <- 7  # "Environmental Politics"

nfam <- 7
mu0 <- matrix(NA, nfam+1, 1)
sigmainv0 <- matrix(NA, nfam+1, 1)

for(i in 1:nfam){
	x <- eurobaro[eurobaro$party_family==i,]
	mu0[i] <- mean(x$lrs)
	sigmainv0[i] <- 1/var(x$lrs)
	}

colnames(mu0) <- c("lrmu0")
rownames(mu0) <- c(seq(1,7), 12)



mu0[nfam+1] <- 0
sigmainv0[nfam+1] <- 0.09



ebprior <- list(eb=eurobaro, mu0=mu0, sigmainv0=sigmainv0, nfam=nfam)


save(ebprior, file="cee_prior.Rdata")